package com.jinyang.jetpackdemo.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IntRange
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.bean.ArticleItem4
import com.jinyang.jetpackdemo.databinding.ActivityRecyclerViewBinding
import com.jinyang.jetpackdemo.databinding.LayoutItemArticleBinding
import com.jinyang.jetpackdemo.util.LjyLogUtil

class RecyclerViewActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRecyclerViewBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_view)
        binding.lifecycleOwner = this
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        val articleList: MutableList<ArticleItem4> = ArrayList()
        articleList.add(ArticleItem4("title1", "jinYang", "content111"))
        articleList.add(ArticleItem4("title2", "jinYang", "content222"))
        articleList.add(ArticleItem4("title3", "jinYang", "content333"))
        binding.recyclerView.adapter = MyAdapter(articleList)
        binding.onClickPresenter3 = OnClickPresenter3()
    }

    inner class OnClickPresenter3 {
        fun setNewList() {
            val articleList: MutableList<ArticleItem4> = ArrayList()
            articleList.add(ArticleItem4("titleNewList1", "jinYang", "content111"))
            articleList.add(ArticleItem4("titleNewList2", "jinYang", "content222"))
            articleList.add(ArticleItem4("titleNewList3", "jinYang", "content333"))
            (binding.recyclerView.adapter as MyAdapter).setNewInstance(articleList)
        }
        fun changeItem() {
            (binding.recyclerView.adapter as MyAdapter).data[0].title.value += "abc"
            (binding.recyclerView.adapter as MyAdapter).data[0].author.value += "abc"
            (binding.recyclerView.adapter as MyAdapter).data[0].content.value += "abc"
            (binding.recyclerView.adapter as MyAdapter).notifyItemChanged(0)
        }

        fun addArticle() {
            (binding.recyclerView.adapter as MyAdapter).addData(
                0,
                ArticleItem4(
                    "title${(binding.recyclerView.adapter as MyAdapter).data.size}",
                    "jinYang",
                    "content${(binding.recyclerView.adapter as MyAdapter).data.size}"
                )
            )
            LjyLogUtil.d("addArticle")
        }

        fun removeArticle() {
            (binding.recyclerView.adapter as MyAdapter).removeAt(0)
            LjyLogUtil.d("removeArticle")
        }
    }
}

class MyAdapter(var data: MutableList<ArticleItem4>) :
    RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.layout_item_article, parent, false)
        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.binding?.articleInfo = data[position]
        holder.binding?.executePendingBindings()
    }

    override fun getItemCount(): Int {
        return data.size
    }

    open fun setNewInstance(list: MutableList<ArticleItem4>?) {
        if (list === this.data) {
            return
        }
        this.data = list ?: arrayListOf()
        notifyDataSetChanged()
    }

    open fun addData(@IntRange(from = 0) position: Int, data: ArticleItem4) {
        this.data.add(position, data)
        notifyItemInserted(position)
    }

    open fun removeAt(@IntRange(from = 0) position: Int) {
        if (position >= data.size) {
            return
        }
        this.data.removeAt(position)
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, this.data.size - position)
    }


    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding: LayoutItemArticleBinding? = DataBindingUtil.bind(itemView)
    }
}